package com.sfzd5.UI;

import com.sfzd5.aiclassroom.Base64ImageUtils;
import com.sfzd5.aiclassroom.modle.Tasksubmit;
import com.sfzd5.exam.helplibrary.exam.ExamInof;
import com.sfzd5.exam.helplibrary.exam.answersheet.AnswerSheetQuestion;
import com.sfzd5.exam.helplibrary.exam.answersheet.Area;
import com.sfzd5.exam.helplibrary.taskstatisitcs.StuInfo;
import com.sfzd5.exam.helplibrary.taskstatisitcs.TaskStatistics;
import org.cef.CefApp;
import org.cef.CefClient;
import org.cef.CefSettings;
import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.handler.CefLifeSpanHandlerAdapter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class PicsDialog extends JDialog {
    private JPanel contentPane;
    private static final long serialVersionUID = 70653778104813836L;
    private CefApp cefApp_;
    private CefClient client_;
    private CefBrowser browser_;
    private Component browerUI_;

    TaskStatistics taskStatistics;
    ExamInof answerSheetExamInof;

    //显示所有学生提交的主观点作业，用html来显示
    public PicsDialog(TaskStatistics taskStatistics, ExamInof answerSheetExamInof) {
        setContentPane(contentPane);
        setModal(true);

        this.taskStatistics = taskStatistics;
        this.answerSheetExamInof = answerSheetExamInof;

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                CefApp.getInstance().dispose();
                dispose();
            }
        });

        initCEF(createHtml());
    }


    private void initCEF(String html) {
        cefApp_ = CefApp.getInstance();
        client_ = cefApp_.createClient();

        browser_ = client_.createBrowser("", false, false);

        browerUI_ = browser_.getUIComponent();
        contentPane.add(browerUI_, BorderLayout.CENTER);

        browser_.loadString(html, "");
    }

    public String createHtml() {
        StringBuilder html = new StringBuilder();
        taskStatistics.stuInfoList.sort(new Comparator<StuInfo>() {
            @Override
            public int compare(StuInfo o1, StuInfo o2) {
                return Long.compare(o1.id, o2.id);
            }
        });

        HashMap<Long, List<Tasksubmit>> tasksubmitMap = taskStatistics.getTasksubmitMap();

        for (StuInfo stu : taskStatistics.stuInfoList) {
            html.append(stu.name);//添加学生姓名
            List<Tasksubmit> tasksubmitList = tasksubmitMap.get(stu.id);
            for (Area area : answerSheetExamInof.areaList) {
                if (area.areaType != Area.AreaType.Choice) {
                    for (AnswerSheetQuestion asq : area.answerSheetQuestionList) {
                        //根据 asq.questionId 查询 TasksubmitList，找到图片
                        Tasksubmit tasksubmit = null;
                        for (Tasksubmit ts : tasksubmitList) {
                            if (ts.student_id == stu.id) {
                                tasksubmit = ts;
                                break;
                            }
                        }
                        if (tasksubmit != null) {
                            html.append(tasksubmit.answerBase64Pic); //添加习题图片
                        }
                    }
                }
            }
        }

        return html.toString();
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new Insets(10, 10, 10, 10), -1, -1));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }
}
